package cn.stylefeng.guns.sys.modular.dbServicelist.controller;

import cn.stylefeng.guns.base.auth.context.LoginContext;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.auth.cache.SessionManager;
import cn.stylefeng.guns.sys.modular.dbServicelist.entity.DbServicelist;
import cn.stylefeng.guns.sys.modular.dbServicelist.entity.Exchange;
import cn.stylefeng.guns.sys.modular.dbServicelist.model.params.DbServicelistParam;
import cn.stylefeng.guns.sys.modular.dbServicelist.model.result.DbServicelistResult;
import cn.stylefeng.guns.sys.modular.dbServicelist.service.DbServicelistService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


/**
 * 目录服务控制器
 *
 * @author zhaoyiheng
 * @Date 2020-08-19 13:58:24
 */
@Controller
@RequestMapping("/databaseListKh")
public class DbServicelistController extends BaseController {

    private String PREFIX = "/modular/dbServicelist";
    private String PREFIX1 = "/modular/dbServicelist/dbServicelist";

    @Autowired
    private DbServicelistService dbServicelistService;
    @Autowired
    private SessionManager sessionManager;

    /**
     * 跳转到主页面
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/dbServicelist.html";
    }

    /**
     * 跳转到查看详情界面
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @RequestMapping("/Details")
    public String Details() {
        return "/modular/dbServicedetails/dbServicedetails.html";
    }

    /**
     * 注册前跳转到交换机界面
     */
    @RequestMapping("/exchangeComputer")
    public String exchangeComputer() {
        return PREFIX + "/exchangeComputer.html";
    }

    /**
     * 跳转到增加有效期页面
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @RequestMapping("/addDataTime")
    public String addDataTime() {
        return PREFIX + "/addDataTime.html";
    }

    /**
     * 新增页面
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/dbServicelist_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/dbServicelist_edit.html";
    }


    /**
     * 跳转到修改密码界面
     */
    @RequestMapping("/alter_password")
    public String alterPassword() {
        return PREFIX + "/changePassword.html";
    }


    /**
     * 新增接口
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(DbServicelistParam dbServicelistParam) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        Integer add = this.dbServicelistService.add(dbServicelistParam, user);
        if (add == 200) {
            return ResponseData.success();
        }
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(DbServicelistParam dbServicelistParam) {
        Integer update = this.dbServicelistService.update(dbServicelistParam);
        if (update == 200) {
            return ResponseData.success();
        } else if (update == 500) {
            return ResponseData.error(update, "数据库中不存在此用户");
        }
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(DbServicelistParam dbServicelistParam) {
        this.dbServicelistService.delete(dbServicelistParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(DbServicelistParam dbServicelistParam) {
        DbServicelist detail = this.dbServicelistService.getById(dbServicelistParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(DbServicelistParam dbServicelistParam) throws ParseException {
//        dbServicelistParam.setSpState("1")
        return this.dbServicelistService.findPageBySpec(dbServicelistParam);
    }

    /**
     * 增加有效期时间
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @ResponseBody
    @RequestMapping("/upDataTime")
    public ResponseData upDataTime(DbServicelistParam dbServicelistParam) throws ParseException {
        this.dbServicelistService.upDataTime(dbServicelistParam);
        return ResponseData.success();
    }


    /**
     * 获取当前登录用户信息
     *
     * @author zhaoyiheng
     * @Date 2020-08-19
     */
    @ResponseBody
    @RequestMapping("/getUser")
    public ResponseData getUser() {

        return ResponseData.success(LoginContextHolder.getContext().getUser());
    }


    /**
     * 跳转到主页面
     *
     * @author author
     * @Date 2020-08-21
     */
    @RequestMapping("/CatalogList")
    public String dbServiceList() {
        return PREFIX1 + "/dbServiceCatalogList.html";
    }

    /**
     * 跳转查看详情页面
     *
     * @author author
     * @Date 2020-08-21
     */
    @RequestMapping("/details")
    public String detail() {
        return PREFIX1 + "/dbService_detail.html";
    }

    /**
     * 查询列表
     *
     * @author author
     * @Date 2020-08-31
     */
    @ResponseBody
    @RequestMapping("/servicelist")
    public LayuiPageInfo servicelist(DbServicelistParam dbServicelistParam) throws ParseException {
        dbServicelistParam.setState("1");
        dbServicelistParam.setOpen("1");
        dbServicelistParam.setSpState("1");
        return this.dbServicelistService.findServicePageBySpec(dbServicelistParam);
    }


    /**
     * 委办局跳转到服务目录已申请列表页面
     *
     * @author author
     * @Date 2020-09-10
     */
    @RequestMapping("/applyList")
    public String dbServiceApplyList() {
        return PREFIX1 + "/dbServiceApplyList.html";
    }

    /**
     * 委办局已申请服务目录列表
     *
     * @author author
     * @Date 2020-09-10
     */
    @ResponseBody
    @RequestMapping("/listApply")
    public LayuiPageInfo listApply(DbServicelistParam dbServicelistParam) throws ParseException {
        int userId = Math.toIntExact(LoginContextHolder.getContext().getUserId());
        dbServicelistParam.setUserId((long) userId);
        return this.dbServicelistService.queryApply(dbServicelistParam);
    }

    /**
     * 测试连接数据库
     *
     * @author author
     * @Date 2020-10-29
     */
    @ResponseBody
    @RequestMapping("/testConnect")
    public String testConnect(DbServicelistParam dbServicelistParam, Exchange exchange) {
        String returnString = this.dbServicelistService.testConnectServer(dbServicelistParam);
        return returnString;
    }

    /**
     * 数据库申请服务器下拉框
     */
    @ResponseBody
    @RequestMapping("/searchServer")
    public List<Exchange> searchServer(Exchange exchange) {
        return this.dbServicelistService.searchExchangeList(exchange);
    }

    /**
     * 通过id查询该数据库详情
     */
    @ResponseBody
    @RequestMapping("/searchServiceList")
    public LayuiPageInfo searchServiceList(DbServicelistParam dbServicelistParam) throws ParseException {
        return this.dbServicelistService.queryServiceListByParam(dbServicelistParam);
    }
}


